package it.yuzuojian.com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.yuzuojian.com.domain.CommonResult;
import it.yuzuojian.com.domain.Singer;
import it.yuzuojian.com.domain.Song;
import it.yuzuojian.com.service.SongService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongService songService;
/*    @PostMapping("/delete")
    public CommonResult deleteSong(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(requestJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0,"json后端转换失败！",false);
        }
        JsonNode id = jsonNode.get("id");
        Integer[] ids={Integer.parseInt(id.toString())};
        if(songService.delete(ids)){
            return new CommonResult<Boolean>(1,"删除成功！",true);
        }
        return new CommonResult<Boolean>(0,"删除失败！",false);
    }*/
    @PostMapping("/delete")
    public CommonResult deleteMoreSong(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(requestJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0,"json后端转换失败！",false);
        }
        JsonNode ids = jsonNode.get("ids");
        String[] split = ids.toString().replace("[", "").replace("]", "")
                .trim().split(",");
        Integer[] data=new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            data[i]=Integer.parseInt(split[i]);
        }
        if(songService.delete(data)){
            return new CommonResult<Boolean>(1,"删除成功！",true);
        }
        return new CommonResult<Boolean>(0,"存在删除失败！",false);
    }
    @PostMapping("/update")
    public CommonResult updateSong(@RequestBody String requestJson){
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        Song song;
        try {
            song = objectMapper.readValue(requestJson, Song.class);
            song.setUpdateTime(new Date());
            System.out.println(song);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0,"json后端转换失败！",false);
        }
        if(songService.update(song)){
            return new CommonResult<Boolean>(1,"修改成功！",true);
        }
        return new CommonResult<Boolean>(0,"修改失败！",false);
    }
    @PostMapping("/add")
    public CommonResult addSong(Song song, MultipartFile file){
        song.setCreateTime(new Date());
        song.setUpdateTime(new Date());
        if(file==null||file.isEmpty()){
            return new  CommonResult<Boolean>(0,"上传文件为空",false);
        }
        String fileName=System.currentTimeMillis()+file.getOriginalFilename();
        String path =System.getProperty("user.dir")+System.getProperty("file.separator")+"music"
                +System.getProperty("file.separator")+"song";
        File file1=new File(path);
        if(file1.exists()){
            file1.mkdir();
        }
        song.setUrl("/song/"+fileName);
        File upload= new File(path,fileName);

        try {
            file.transferTo(upload);
            if(songService.insert(song)){
                return new CommonResult<Boolean>(1,"添加成功！",true);
            }else {
                return new CommonResult<Boolean>(0,"添加失败！",false);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0,"文件上传失败！",false);
        }
    }
    @GetMapping("/findBySingerId")
    public CommonResult findBySingerId(Integer singerId){
        List<Song> songs = null;
        try {
            songs= songService.selectBySingerId(singerId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<Song>>(0,"查询失败！",songs);
        }
        return new CommonResult<List<Song>>(1,"查询成功！",songs);
    }
    @GetMapping("/findByName")
    public CommonResult findByName(String name){
        List<Song> songs = null;
        System.out.println(name);
        try {
            songs= songService.selectByName(name);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<Song>>(0,"查询失败！",songs);
        }
        return new CommonResult<List<Song>>(1,"查询成功！",songs);
    }
    @GetMapping("/findOneByName")
    public CommonResult findOneByName(String name){
        Song song = null;
        System.out.println(name);
        try {
            song= songService.selectOneByName(name);

            if(song==null){
                return new CommonResult<Song>(0,"不存在该歌曲！",song);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<Song>(0,"查询失败！",song);
        }
        return new CommonResult<Song>(1,"查询成功！",song);
    }
    @GetMapping("/findAll")
    public CommonResult findAll(){
        
        List<Song> songs = null;
        try {
            songs= songService.selectAll();
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<Song>>(0,"查询失败！",songs);
        }
        return new CommonResult<List<Song>>(1,"查询成功！",songs);
    }
    @GetMapping("/findById")
    public CommonResult findById(Integer id){
        Song song = null;
        System.out.println(id);
        try {
            song= songService.selectById(id);
            System.out.println(song);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<Song>(0,"查询失败！",song);
        }
        return new CommonResult<Song>(1,"查询成功！",song);
    }

    @PostMapping("/updateSongPic")
    public CommonResult updateSongPic(MultipartFile file, Integer id) throws IOException {
        if(file==null||file.isEmpty()){
            return new  CommonResult<Boolean>(0,"上传文件为空",false);
        }
        String fileName=System.currentTimeMillis()+file.getOriginalFilename();
        String path =System.getProperty("user.dir")+System.getProperty("file.separator")+"music"
                +System.getProperty("file.separator")+"img"+ System.getProperty("file.separator")
                +"songPic"/*+System.getProperty("file.separator")*/;
        File file1=new File(path);
        if(file1.exists()){
            file1.mkdir();
        }
        File upload=new File(path,fileName);
        System.out.println("path = " + path);
        System.out.println("file:"+upload.getCanonicalPath());
        String pic = "/img/songPic/"+fileName;
        try {
            file.transferTo(upload);
            Song song = new Song();
            song.setId(id);
            song.setPic(pic);
            songService.update(song);
        } catch (IOException e) {
            e.printStackTrace();
            return new  CommonResult<Boolean>(0,"文件上传失败",false);
        }
        return new  CommonResult<Boolean>(1,"文件上传成功",true);

    }

    @PostMapping("/updateSongUrl")
    public CommonResult updateSongUrl(MultipartFile file, Integer id) throws IOException {
        if(file==null||file.isEmpty()){
            return new  CommonResult<Boolean>(0,"上传文件为空",false);
        }
        String fileName=System.currentTimeMillis()+file.getOriginalFilename();
        String path =System.getProperty("user.dir")+System.getProperty("file.separator")+"music"
                +System.getProperty("file.separator")+"song";
        File file1=new File(path);
        if(file1.exists()){
            file1.mkdir();
        }
        File upload=new File(path,fileName);
        System.out.println("path = " + path);
        System.out.println("file:"+upload.getCanonicalPath());
        String url = "/song/"+fileName;
        try {
            file.transferTo(upload);
            Song song = new Song();
            song.setId(id);
            song.setUrl(url);
            songService.update(song);
        } catch (IOException e) {
            e.printStackTrace();
            return new  CommonResult<Boolean>(0,"文件上传失败",false);
        }
        return new  CommonResult<Boolean>(1,"文件上传成功",true);

    }

}
